package com.clkj.xiuzheng.dao.shangcheng;


import com.clkj.xiuzheng.pojo.shangcheng.ProductCompany;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

@Repository
public interface ScCompanyMapper extends Mapper<ProductCompany> {
    @Select({
            "<script>",
            "SELECT p.*,count( DISTINCT u.user_id) as userNum,count( DISTINCT c.code_id) as codeNum,sum(DISTINCT uc.total_money) as totalMoney ,sum(DISTINCT cc.grant_money) as totalGrant FROM t_xz_productCompany p left join t_xz_user u on p.company_id = u.company_id left join  t_xz_PromoCode c on p.company_id = c.company_id left join t_xz_userCommission uc on u.user_id =  uc.user_id left join t_xz_companyCommisson  cc on c.company_id=cc.company_id  ",
            "where p.parent_id=#{companyId}",
            "<if test = 'companyName!=null'>",
            "and  p.company_name like '%${companyName}%'",
            "</if>",
            "<if test = 'startTime!=null'>",
            "and  p.create_time &gt;= #{startTime} ",
            "</if>",
            "<if test = 'endTime!=null'>",
            "and  p.create_time &lt;= #{endTime} ",
            "</if>",
            "group by p.company_id,p.company_name,p.company_status,p.company_count,p.company_password,p.create_time,p.parent_id",
            "</script>"
    })
    List<ProductCompany> getCompanyList(@Param("companyName") String companyName, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("companyId") Integer companyId);
    @Select({
            "<script>",
            "SELECT sum(cc.grant_money) AS totalGrant, p.company_name, p.company_id",
            "FROM t_xz_productCompany p",
            "LEFT JOIN t_xz_companyCommisson cc ON cc.company_id = p.company_id",
            "WHERE p.parent_id = #{companyId}",
            "<if test='companyName != null and  companyName != \"\"'>",
            "AND p.company_name LIKE CONCAT('%', #{companyName}, '%')",
            "</if>",
            "<if test='startTime != null and endTime !=null'>",
            "AND cc.grant_date &gt;= #{startTime} AND cc.grant_date &lt;= #{endTime}",
            "</if>",
            "GROUP BY p.company_id, p.company_name",
            "</script>"
    })
    List<ProductCompany> getCompanyListInfo(@Param("companyName") String companyName, @Param("startTime") String startTime, @Param("endTime") String endTime, @Param("companyId") Integer companyId);
    @Select("select company_id from t_xz_productCompany where parent_id = #{companyId} ")
    List<Integer> getCompanyId(@Param("companyId") Integer companyId);
}
